package org.gd.hellojaxb.privat24.other;

import org.gd.hellojaxb.utils.Marshall;
import org.gd.hellojaxb.utils.UnMarshall;

import javax.xml.bind.JAXBException;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/*
    <?xml version="1.0" encoding="UTF-8"?>
    <doc>
        <r t="OperationStorage">
            <i dateTo="20121212"  dateFrom="20121210"  ClientID="47824169" />
        </r>
    </doc>
*/

@XmlRootElement(name = "doc")
public class OperationStorageRequest {

    @XmlRootElement(name = "r")
    private static class Request {

        @XmlRootElement(name = "i")
        private static class Item {
            private String dateTo;
            private String dateFrom;
            private String ClientID;

            public Item() {

            }

            public Item(String ClientID, String dateFrom, String dateTo) {
                this.dateTo = dateTo;
                this.dateFrom = dateFrom;
                this.ClientID = ClientID;
            }

            public String getClientID() {
                return ClientID;
            }
            @XmlAttribute(name = "ClientID")
            public void setClientID(String clientID) {
                ClientID = clientID;
            }

            public String getDateTo() {
                return dateTo;
            }
            @XmlAttribute(name = "DATE_TO")
            public void setDateTo(String dateTo) {
                this.dateTo = dateTo;
            }

            public String getDateFrom() {
                return dateFrom;
            }
            @XmlAttribute(name = "DATE_FROM")
            public void setDateFrom(String dateFrom) {
                this.dateFrom = dateFrom;
            }
        }

        private List<Item> item = new ArrayList<Item>();
        private String serviceName = "OperationStorage";

        public Request() {

        }

        public Request(String ClientID, String DATE_FROM, String DATE_TO) {
            this.item.add(new Item(ClientID, DATE_FROM, DATE_TO));
        }

        public List<Item> getItem() {
            return item;
        }
        @XmlElement
        public void setItem(List<Item> item) {
            this.item = item;
        }

        public String getServiceName() {
            return serviceName;
        }
        @XmlAttribute(name = "t")
        public void setServiceName(String serviceName) {
            this.serviceName = serviceName;
        }

        public void addItem(String ClientID, String DATE_FROM, String DATE_TO) {
            this.item.add(new Item(ClientID, DATE_FROM, DATE_TO));
        }
    }

    private Request request;

    public OperationStorageRequest() {
        request = new Request();
    }

    public OperationStorageRequest(String ClientID, String DATE_FROM, String DATE_TO) {
        request = new Request(ClientID, DATE_FROM, DATE_TO);
    }

    public Request getRequest() {
        return request;
    }
    @XmlElement(name = "r")
    public void setRequest(Request request) {
        this.request = request;
    }
    public static void main(String...args) throws JAXBException, IOException {
        OperationStorageRequest osr = new OperationStorageRequest("1","2","3");
        String xml;
        System.out.println(xml = Marshall.getInstance().marshal(osr));

        OperationStorageRequest newOsr = UnMarshall.unmarshal(xml, OperationStorageRequest.class);

        System.out.println(
                newOsr.getRequest().getItem().get(0).getClientID() + " - " +
                        newOsr.getRequest().getItem().get(0).getDateFrom() + " - " +
                        newOsr.getRequest().getItem().get(0).getDateTo()
        );
        System.exit(0);
    }
}
